package cn.fover.utils;

import org.apache.http.Header;
import org.json.JSONException;

import cn.fover.utils.i.IConnect;
import cn.fover.utils.i.IJson;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class NetUtils {
	private static String url;

	/**
	 * 设置默认URL 减少每次输入麻烦
	 *
	 * @param url
	 *            url
	 */
	public static void setDefaultUrl(String url) {
		NetUtils.url = url;
	}

	/**
	 * get方式请求网络数据
	 *
	 * @param Url
	 *            请求的url
	 * @param params
	 *            参数
	 * @param iConnect
	 *            回调
	 */
	public static void get(String Url, RequestParams params,
			final IConnect iConnect) {
		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.get(Url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				iConnect.onSucceed(msg);
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				String msg = new String(arg3.getMessage());
				iConnect.onFailed(msg);
			}
		});
	}

	/**
	 * get 方式请求网络数据
	 *
	 * @param params
	 * @param iConnect
	 */
	public static void get(RequestParams params, final IConnect iConnect) {
		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.get(url, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				iConnect.onSucceed(msg);
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				String msg = new String(arg3.getMessage());
				iConnect.onFailed(msg);
			}
		});
	}

	/**
	 * get方式获取Json数据并返回Json
	 *
	 * @param Url
	 *            请求的地址
	 * @param params
	 *            属性
	 * @param obj
	 *            需要转换的对象
	 * @param iJson
	 *            回调
	 */
	public static <T> void getJsonObject(String Url, RequestParams params,
			final Class<T> obj, final IJson iJson) {

		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.get(Url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				iJson.onSucceed(JSON.parseObject(msg, obj));
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				iJson.onFailed(arg3.getMessage());
			}
		});
	}

	/**
	 * 通过Get方式获取数据并返回Json
	 *
	 * @param params
	 *            请求的参数
	 * @param obj
	 *            对象
	 * @param iJson
	 *            回调
	 */
	public static <T> void getJsonObject(RequestParams params,
			final Class<T> obj, final IJson iJson) {

		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.get(url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				iJson.onSucceed(JSON.parseObject(msg, obj));
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				iJson.onFailed(arg3.getMessage());
			}
		});
	}

	/**
	 * Get方式请求数据返回Json数组
	 *
	 * @param Url
	 *            Url 地址
	 * @param params
	 *            请求参数
	 * @param obj
	 *            请求对象
	 * @param iJson
	 *            回调
	 */
	public static <T> void getJsonArray(String Url, RequestParams params,
			final Class<T> obj, final String filter, final IJson iJson) {

		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.get(Url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				try {
					org.json.JSONObject jsonObject = new org.json.JSONObject(
							msg);
					String data = jsonObject.getString(filter);
					iJson.onSucceed(JSON.parseArray(data, obj));
				} catch (JSONException e) {
					iJson.onFailed("do not get filter json data");
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				iJson.onFailed(arg3.getMessage());
			}
		});
	}

	/**
	 * Get方式请求数据并返回Json数组
	 *
	 * @param params
	 *            参数
	 * @param obj
	 *            对象
	 * @param filter
	 *            提取的值
	 * @param iJson
	 *            回调
	 */
	public static <T> void getJsonArray(RequestParams params,
			final Class<T> obj, final String filter, final IJson iJson) {

		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.get(url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				try {
					org.json.JSONObject jsonObject = new org.json.JSONObject(
							msg);
					String data = jsonObject.getString(filter);
					iJson.onSucceed(JSON.parseArray(data, obj));
				} catch (JSONException e) {
					iJson.onFailed("Do not get filter json data");
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				iJson.onFailed(arg3.getMessage());
			}
		});
	}

	/**
	 * Get方式请求数据并返回JsonArray
	 *
	 * @param Url
	 *            请求的地址
	 * @param params
	 *            参数
	 * @param obj
	 *            对象
	 * @param iJson
	 *            回调
	 */

	public static <T> void getJsonArray(String Url, RequestParams params,
			final Class<T> obj, final IJson iJson) {

		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.get(Url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				iJson.onSucceed(JSON.parseArray(msg, obj));
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				iJson.onFailed(arg3.getMessage());
			}
		});
	}

	/**
	 * Get方式请求数据并返回Json数组
	 *
	 * @param params
	 *            参数
	 * @param obj
	 *            对象
	 * @param iJson
	 *            回调
	 */
	public static <T> void getJsonArray(RequestParams params,
			final Class<T> obj, final IJson iJson) {

		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.get(url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				iJson.onSucceed(JSON.parseArray(msg, obj));
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				iJson.onFailed(arg3.getMessage());
			}
		});
	}

	/**
	 * Post方式请求网络数据
	 *
	 * @param Url
	 *            url
	 * @param params
	 *            参数
	 * @param iConnect
	 *            回调
	 */
	public static void post(String Url, RequestParams params,
			final IConnect iConnect) {
		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.post(Url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				iConnect.onSucceed(msg);
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				String msg = new String(arg3.getMessage());
				iConnect.onFailed(msg);
			}
		});
	}

	/**
	 * Post方式请求网络数据
	 *
	 * @param params
	 *            参数
	 * @param iConnect
	 *            回调
	 */
	public static void post(RequestParams params, final IConnect iConnect) {
		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.post(url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				iConnect.onSucceed(msg);
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				String msg = new String(arg3.getMessage());
				iConnect.onFailed(msg);
			}
		});
	}

	/**
	 * Post请求数据并返回Json对象
	 *
	 * @param Url
	 *            URL
	 * @param params
	 *            参数
	 * @param obj
	 *            对象
	 * @param iJson
	 *            回调
	 */
	public static <T> void postJsonObject(String Url, RequestParams params,
			final Class<T> obj, final IJson iJson) {

		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.post(Url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				iJson.onSucceed(JSON.parseObject(msg, obj));
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				iJson.onFailed(arg3.getMessage());
			}
		});
	}

	/**
	 * Post请求数据并返回Json对象
	 *
	 * @param params
	 *            参数
	 * @param obj
	 *            对象
	 * @param iJson
	 *            回调
	 */
	public static <T> void postJsonObject(RequestParams params,
			final Class<T> obj, final IJson iJson) {

		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.post(url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				iJson.onSucceed(JSON.parseObject(msg, obj));
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				iJson.onFailed(arg3.getMessage());
			}
		});
	}

	/**
	 * Post 方式请求数据并返回Json数组
	 *
	 * @param Url
	 *            url
	 * @param params
	 *            参数
	 * @param obj
	 *            对象
	 * @param filter
	 *            数据提取值
	 * @param iJson
	 *            回调
	 */
	public static <T> void postJsonArray(String Url, RequestParams params,
			final Class<T> obj, final String filter, final IJson iJson) {

		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.post(Url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				try {
					org.json.JSONObject jsonObject = new org.json.JSONObject(
							msg);
					String data = jsonObject.getString(filter);
					iJson.onSucceed(JSON.parseArray(data, obj));
				} catch (JSONException e) {
					iJson.onFailed("Do not get filter json data");
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				iJson.onFailed(arg3.getMessage());
			}
		});
	}

	/**
	 * Post方式请求数据并返回Json数组
	 *
	 * @param params
	 *            参数
	 * @param obj
	 *            对象
	 * @param filter
	 *            过滤
	 * @param iJson
	 *            回调
	 */
	public static <T> void postJsonArray(RequestParams params,
			final Class<T> obj, final String filter, final IJson iJson) {

		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.post(url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				try {
					org.json.JSONObject jsonObject = new org.json.JSONObject(
							msg);
					String data = jsonObject.getString(filter);
					iJson.onSucceed(JSON.parseArray(data, obj));
				} catch (JSONException e) {
					iJson.onFailed("Do not get filter json data");
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				iJson.onFailed(arg3.getMessage());
			}
		});
	}

	/**
	 * Post方式请求数据并返回Json数组
	 *
	 * @param Url
	 *            url
	 * @param params
	 *            参数
	 * @param obj
	 *            对象
	 * @param iJson
	 *            回调
	 */
	public static <T> void postJsonArray(String Url, RequestParams params,
			final Class<T> obj, final IJson iJson) {

		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.get(Url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				iJson.onSucceed(JSON.parseArray(msg, obj));
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				iJson.onFailed(arg3.getMessage());
			}
		});
	}

	/**
	 * Post请求网络参数并返回Json数组
	 * 
	 * @param params
	 *            参数
	 * @param obj
	 *            对象
	 * @param iJson
	 *            回调
	 */
	public static <T> void postJsonArray(RequestParams params,
			final Class<T> obj, final IJson iJson) {

		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.get(url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String msg = new String(arg2);
				iJson.onSucceed(JSON.parseArray(msg, obj));
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				iJson.onFailed(arg3.getMessage());
			}
		});
	}

}
